﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Abreu.Base.ViewModels.Interfaces;
using System.Windows;
using System.Windows.Media;

namespace Abreu.Base.ViewModels.Managers
{
    public delegate void CloseButtonClick(object sender, EventArgs e);
 

    public class TabContainerItem : TabItem
    {

        public event CloseButtonClick CloseClicked;
      
        public TabContainerItem(string headerTitle, UserControl tabContainerUC, IViewModel viewModel)
        {
            this.Header = this.SetHeader(headerTitle);
            this.IsSelected = true;
            this.Content = tabContainerUC;
            tabContainerUC.DataContext = viewModel;            
        }


        public TabContainerItem()
        {

        }

        internal Type GetContentType()
        {
            return ((UserControl)this.Content).DataContext.GetType(); 
        }



        internal IViewModel GetContentDataContext()
        {
            return ((UserControl)this.Content).DataContext as IViewModel;
        }


        internal object SetHeader(string headerText)
        {
            Grid g = new Grid();
            ColumnDefinition headerCol = new ColumnDefinition();
            headerCol.Width = GridLength.Auto;
            ColumnDefinition closeButtonCol = new ColumnDefinition();
            closeButtonCol.Width = GridLength.Auto;
            g.ColumnDefinitions.Add(headerCol);
            g.ColumnDefinitions.Add(closeButtonCol);

            TextBlock tb = new TextBlock();
            tb.Text = headerText;
            tb.SetValue(Grid.ColumnProperty, 0);

            Button b = new Button();
            b.Content = "X";

            b.BorderBrush = new SolidColorBrush(Colors.Transparent);
            b.FontWeight = FontWeights.Bold;
            b.Background = new SolidColorBrush(Colors.Transparent);
            b.Foreground = new SolidColorBrush(Colors.Gray);
            b.Margin = new Thickness(50, 0, 0, 0);
            b.Click += new RoutedEventHandler(b_Click);

            b.SetValue(Grid.ColumnProperty, 1);

            g.Children.Add(tb);
            g.Children.Add(b);

            return g;


        }

        void b_Click(object sender, RoutedEventArgs e)
        {
            if (CloseClicked != null)
            {
                this.CloseClicked(sender, e);
            }
        }

      
    }
}
